var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:[],
    yunfei:0,
    totalPay:0,
    belongto:0,
      isExistAddress:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var orderInfo=wx.getStorageSync('goodsInfo');
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
      orderInfo:orderInfo,
      belongto:orderInfo.mallGoodsParamJson.belongTo
    })
    if(that.data.orderInfo.mallGoodsParamJson.totalPrice<199){
      that.setData({
        yunfei:10,
        totalPay:that.data.orderInfo.mallGoodsParamJson.totalPrice
      })
    }else{
      that.setData({
        yunfei:0,
        totalPay:that.data.orderInfo.mallGoodsParamJson.totalPrice
      })
    }
    var json={memberId:this.data.userInfo.id}
    wx.request({
      url: app.globalData.baseUrl+'/member/receivingAddress/findReceivingAddressByMemberId',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(json),
      success:function(res){
        if(Number(res.data.length)>0 && res.statusCode==200) {
          that.setData({
            address: res.data[0],
              isExistAddress:true
          })
        }else{
          that.setData({
            addressaddress:[],
              isExistAddress:false
          })
        }
      }
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,1);
    }
  },
//立即结算
  buy:function(){
    var that=this;
    if(!that.data.isExistAddress){
        wx.showModal({
            title: '您还没有添加收货地址哦,赶紧去添加收货地址吧？',
            content: '',
            success: function (res) {
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/my/address?type=add'
                    })
                }
            }
        });
        return false;
    }
    var buyNowStockParamJson= {
      buyNumber: that.data.orderInfo.mallGoodsParamJson.buyNumber,
      isSpecificationGoods: that.data.orderInfo.mallGoodsParamJson.isSpecificationGoods,
      goodsId: that.data.orderInfo.mallGoodsParamJson.goodsId,
      goodsSpecificationId: that.data.orderInfo.mallGoodsParamJson.goodsSpecificationId
    }
    wx.request({
      url: app.globalData.baseUrl + '/order/reduceStock',
      header: app.globalData.headerPost,
      method: 'POST',
      data: JSON.stringify(buyNowStockParamJson),
      success: function (res) {
        if(res.data.status==200){
          //跳转
          wx.redirectTo({
            url: '/pages/shopcar/buy?belongTo='+that.data.orderInfo.orderInfo.belongTo+"&totalPrice="+(Number(that.data.orderInfo.mallGoodsParamJson.totalPrice)+Number(that.data.yunfei))+"&orderId="+that.data.orderInfo.orderInfo.orderId+"&orderNumber="+that.data.orderInfo.orderInfo.orderNumber+"&goodsname="+that.data.orderInfo.mallGoodsParamJson.goodsName
          })
        }
      }
    })

  },
  //减
  reduce:function(){
    var that=this;
    var orderTemp=that.data.orderInfo;

    var buyNumber=orderTemp.mallGoodsParamJson.buyNumber;
    if(buyNumber>1){
      buyNumber--;
    }else{
      return false;
    }
    var yf=0;
    var totalP=buyNumber*orderTemp.mallGoodsParamJson.goodsPrice;
    orderTemp.mallGoodsParamJson.buyNumber=buyNumber;
    orderTemp.mallGoodsParamJson.totalPrice=totalP;
    orderTemp.mallGoodsParamJson.totalConsumerMoney=totalP;
    if(totalP<199){
      yf=10;
    }

    var mallGoodsBuyNowOrderUpdateParamJson={
      isSpecificationGoods: that.data.orderInfo.mallGoodsParamJson.isSpecificationGoods,
      orderId:orderTemp.orderInfo.orderId,
      buyNumber:orderTemp.mallGoodsParamJson.buyNumber,
      goodsPrice:orderTemp.mallGoodsParamJson.goodsPrice,
      paymentConsumerMoney:orderTemp.mallGoodsParamJson.paymentConsumerMoney
    }
    wx.request({
      url: app.globalData.baseUrl+'/order/mallGoodsBuyNowOrderUpdate',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(mallGoodsBuyNowOrderUpdateParamJson),
      success:function(res) {
        if(res.data.status==200){
          that.setData({
            orderInfo:orderTemp,
            yunfei:yf,
            totalPay:totalP
          })
        }
      }
    })
  },
  //加
  add:function(){
    var that=this;
    var orderTemp=that.data.orderInfo;
    var buyNumber=orderTemp.mallGoodsParamJson.buyNumber;
    if(buyNumber<orderTemp.kucun){
      buyNumber++;
    }else{
      return false;
    }
    var yf=0;
    var totalP=buyNumber*orderTemp.mallGoodsParamJson.goodsPrice;
    orderTemp.mallGoodsParamJson.buyNumber=buyNumber;
    orderTemp.mallGoodsParamJson.totalPrice=totalP;
    orderTemp.mallGoodsParamJson.totalConsumerMoney=totalP;
    if(totalP<199){
      yf=10;
    }

    var mallGoodsBuyNowOrderUpdateParamJson={
      orderId:orderTemp.orderInfo.orderId,
      buyNumber:orderTemp.mallGoodsParamJson.buyNumber,
      goodsPrice:orderTemp.mallGoodsParamJson.goodsPrice,
      paymentConsumerMoney:orderTemp.mallGoodsParamJson.paymentConsumerMoney
    }
    wx.request({
      url: app.globalData.baseUrl+'/order/mallGoodsBuyNowOrderUpdate',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(mallGoodsBuyNowOrderUpdateParamJson),
      success:function(res){
        if(res.data.status==200){
          that.setData({
            orderInfo:orderTemp,
            yunfei:yf,
            totalPay:totalP
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})